#include "imgui_Custom.h"

bool ImGui::M_SwitchButton(const char* str_id, bool* v, const ImVec2& size) {
    ImGuiWindow* window = GetCurrentWindow();
    if (window->SkipItems)
        return false;

    ImGuiContext& g = *GImGui;
    const ImGuiStyle& style = g.Style;
    const ImGuiID id = window->GetID(str_id);
	ImVec4* colors = ImGui::GetStyle().Colors;
    ImVec2 pos = window->DC.CursorPos;
	ImDrawList* draw_list = ImGui::GetWindowDrawList();
	
	const float gui_height = ImGui::GetFrameHeight();
    const ImVec2 label_size = CalcTextSize(str_id, NULL, true);
	float width = gui_height * size.x;
	float height = gui_height * size.y;
    const ImRect total_bb(pos, ImVec2(pos.x + (width * 1.15f) + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), pos.y + label_size.y + style.FramePadding.y * 2.0f));
    ItemSize(total_bb, style.FramePadding.y);
    if (!ItemAdd(total_bb, id))
        return false;


    bool hovered, held;
    bool pressed = ButtonBehavior(total_bb, id, &hovered, &held);
    if (pressed) {
        *v = !(*v);
        MarkItemEdited(id);
    }

	if (height >= label_size.y) {
    	float fg = (height - label_size.y) / 2;
    	RenderText(ImVec2(pos.x + (width * 1.15f),  pos.y+fg), str_id);
    }
    
	if (held) {
		draw_list->AddRectFilled(pos, ImVec2(pos.x + width, pos.y + height), ImGui::GetColorU32(*v ? colors[ImGuiCol_ButtonHovered] : ImVec4(0.78f, 0.78f, 0.78f, 1.0f)), height * 0.80f);
	} else {
		//draw_list->AddRectFilled(pos, ImVec2(pos.x + width, pos.y + height), ImGui::GetColorU32(*v ?  colors[ImGuiCol_ButtonHovered] : ImVec4(0.78f, 0.78f, 0.78f, 1.0f)), height * 0.80f);
		draw_list->AddRectFilled(pos, ImVec2(pos.x + width, pos.y + height), ImGui::GetColorU32(ImVec4(0.78f, 0.78f, 0.78f, 1.0f)), height * 0.80f);
	}
	draw_list->AddCircleFilled(ImVec2(pos.x + (width * 0.15f) + ((*v ? 1 : 0) * (width * 0.69f)), (pos.y + height * 0.50f)), (height * 0.435f), ImGui::GetColorU32(*v ? colors[ImGuiCol_ButtonActive] : ImVec4(255, 255, 255, 255)));
	return *v;
}


bool ImGui::ButtonTextColored(const ImVec4& col, const char* fmt, ...) {
    va_list args;
    va_start(args, fmt);
    TextColoredV(col, fmt, args);
    va_end(args);
    return IsItemClicked();
}

bool ImGui::M_shut(const char* text) {
    const float w = ImGui::GetWindowWidth();
    const ImVec2 text_size = CalcTextSize(text, NULL, true);
    ImGui::SetCursorPosX(w - (text_size.x + ImGui::GetFrameHeight()/4));
    if (ImGui::SmallButton(text))
        return true;
    else 
        return false;
}

void ImGui::M_shut(const char* text, bool* close) {
    const float w = ImGui::GetWindowWidth();
    const ImVec2 text_size = CalcTextSize(text, NULL, true);
    ImGui::SetCursorPosX(w - (text_size.x + ImGui::GetFrameHeight()/4));
    if (ImGui::SmallButton(text))
       *close = true;
}
void ImGui::M_shut_G(const char* text, bool* close, const ImVec4& col) {
    const float w = ImGui::GetWindowWidth();
    const ImVec2 text_size = CalcTextSize(text, NULL, true);
    ImGui::SetCursorPosX(w - (text_size.x + ImGui::GetFrameHeight()/4));
    if (ImGui::ButtonTextColored(col, text)) {
       *close = !*close;
    }
}
